package jevon.philautus.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import jevon.philautus.domain.Pager;
import jevon.philautus.domain.Role;
import jevon.philautus.service.RoleService;
import jevon.philautus.stereotype.Log;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/role")
public class RoleController {
	@Resource
	private RoleService roleService;
	
	@Log
	private Logger logger;

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	@RequiresPermissions("role:new")
	public String addNew(Model model) {
		logger.debug("add new role");
		model.addAttribute(new Role());
		return "/role/new";
	}

	@RequestMapping(value = "/new", method = RequestMethod.POST)
	@RequiresPermissions("role:new")
	public String create(@Valid Role role, BindingResult result) {
		if (result.hasErrors()) {
			return "/role/new";
		}
		roleService.save(role);
		return "redirect:/role";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) {
		model.addAttribute(roleService.list(new Pager<Role>()));
		return "/role/index";
	}
}
